// pages/find/index.js
var t = getApp(),
  c = t.requirejs("api/index"),
  a = t.requirejs("core");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    route: "find",
    menu: t.tabBar,
    menu_show: false,
    loaded: false,
    list: [],
    page: 1,
    class_id: '0',
    cate_index: '',
    cate: [],
    class_style: 2
  },
  //底部导航跳转
  menu_url: function (k) {
    a.menu_url(k, 2);
  },
  onLoad: function (e) {
    var that=this;
    a.setting();
    if (e != null && e != undefined) {
      that.setData({
        tabbar_index: e.tabbar_index ? e.tabbar_index : -1
      });
    }
      that.setData({
      menu: getApp().tabBar,
      class_id: e.id ? e.id : '0',
    })

      if (that.data.tabbar_index >= 0) {
        that.setData({
        showtabbar: true,
      })
    }
      that.getlist();
      that.initCategory();
  },
  cate_select: function (t) {

    console.log(t);

    var that = this,
      cate = that.data.cate,
      data = a.pdata(t),
      e = {};
    console.log(data)

    e['cate_index'] = data.index;
    e['class_id'] = data.id+"";
    e.list = [];
    e.page = 1;
    e.loaded = false;

    console.log(e);

    that.setData(e)
    that.getlist();
  },
  /**
*获取分类
* @return array
*/
  initCategory: function () {
    var t = this;
    a.get("product/productClass", {}, function (a) {
      t.setData({
        cate: a.info,
      })
    })
  },
  /**
     * 获取文章列表信息
     */
  getlist: function () {
    var that = this,
      class_id = that.data.class_id,
      page = that.data.page;

    var e = {};
    a.get("product/product_list", {
      page: page,
      class_id: class_id
    }, function (t) {
      if (t.code == 0) {
        if (t.info) {
          t.info.length > 0 && (e.page = page + 1, e.list = that.data.list.concat(t.info), t.info.length < 10 && (e.loaded = true));
          t.info.length == 0 && (e.loaded = true);
          e.show = true;
        } else {
          e.loaded = true;
          e.show = true;
        }

        // 使用 decodeURIComponent 的时候报错 Uncaught URIError: URI malformed,因为字符串中可能包含%,需要对其进行转义
        var title = t.info.class_name.replace(/%/g,'%25');
        wx.setNavigationBarTitle({
          title: t.info.class_name ? decodeURIComponent(title) : '产品列表'
        })

        that.setData(e);

      } else {
        a.alert(t.msg, function () {
          return;
        })
        return;
      }
      
    }, true)

  },
  /**
    * 下拉刷新
    */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      page: 1,
      loaded: false
    })
    this.getlist();
    this.initCategory();
    wx.stopPullDownRefresh();
  },
  /**
   *上拉加载
   */
  onReachBottom: function () {
    console.log('加载更多')
    this.data.loaded || this.getlist()
  },
  to_url: function (e) {
    var data = a.pdata(e), url = '';
    if (data.link) {
      url = '/yb_mingpian/pages/web/index?url=' + escape(data.link) + '&name=' + data.name;
    } else {
      url = '/yb_mingpian/pages/find_info/index?id=' + data.id;
    }
    a.jump(url);
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //
  // }
})